package com.security.inteceter;

import com.security.validator.Oauth2ParamsValidator;
import com.security.vo.OauthBaseVo;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by 陈圣融 on 2017-07-28.
 */
@Deprecated
public class OauthHandlerInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        PathMatcher pathMatcher = new AntPathMatcher();
        if (pathMatcher.match(request.getContextPath() + "/oauth/token", request.getRequestURI())) {
            OauthBaseVo oauthBaseErrorVo = Oauth2ParamsValidator.validate(request.getParameterMap());
            if (oauthBaseErrorVo!=null) {
                request.setAttribute("oauthBaseErrorVo", oauthBaseErrorVo);
                request.getRequestDispatcher("/unauthenticated").forward(request, response);
                return false;
            }
        }
        System.out.println("preHandle = " + response.getStatus());
        return super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle处理结果 = " + response.getStatus());
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion处理结果 = " + response.getStatus());
    }
}
